
package com.midrange.rse_enhancements.chgmsg;

/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * Stand alone message formatter.
 * 
 * @author David Gibbs
 *
 */
public class MessageFormatter {

	private static final int MODE_N = 1;
	private static final int MODE_P = 2;
	private static final int MODE_B = 3;
	
	public static final int DEFAULT_WIDTH = 79;
	
	/**
	 * Formats AS400 message text to fit in the default width.
	 * 
	 * @param unformattedText text to be formatted
	 * @return formatted text
	 */
	public static String format(String unformattedText) {
		return format(unformattedText, DEFAULT_WIDTH);
	}
	
	/**
	 * Formats AS400 message text to fit in the specified width.
	 * 
	 * @param unformattedText text to be formatted
	 * @param width text width to be used for word wrapping
	 * @return formatted text
	 */
	public static String format(String unformattedText, int width) {
		
		StringBuffer formattingBuffer = new StringBuffer("  ");
		
		int mode = -1;
		
		int lineLength = 0;
		
		String[] splitText = unformattedText.split("(?<=[\\s])");
		
		for (String word : splitText) {

			if (word.equals("&N ")) {
				mode = MODE_N;
				formattingBuffer.append("\n  "); // 2 spaces
				lineLength = 0;
			} else if (word.equals("&P ")) {
				mode = MODE_P;
				formattingBuffer.append("\n      "); // 6 spaces
				lineLength = 0;
			} else if (word.equals("&B ")) {
				mode = MODE_B;
				formattingBuffer.append("\n    "); // 4 spaces
				lineLength = 0;
			} else {
				if (lineLength + word.length() > width) {
					formattingBuffer.append("\n");
					
					switch (mode) {
					case MODE_N :
					case MODE_P :
						formattingBuffer.append("    "); // 4 spaces
						lineLength = 4;
						break;
					case MODE_B :
						formattingBuffer.append("      "); // 6 spaces
						lineLength = 6;
						break;
					default : 
						formattingBuffer.append("   "); // 2 spaces
						lineLength = 4;
						break;
					}
				}
				
				if (word.length()> 0) {
					formattingBuffer.append(word); // + " ");
				}
				
				lineLength += word.length() + 1;
			}
		}
		
		String formattedText = formattingBuffer.toString();
		
		return formattedText;
	}

	/** 
	 * Formats text containing message formatting characters to be read by
	 * humans by replacing single & with &&
	 * @param test
	 * @return
	 */
	public static String formatForHumans(String test) {
	
		String humanCommand = test.replaceAll("&","&&");
		
		return humanCommand;
	}
	

	
}
